package com.leenmvc.core.web.controller;


import com.leenmvc.core.annotation.ApiDescription;
import com.leenmvc.core.annotation.Note;
import com.leenmvc.core.base.BaseController;
import com.leenmvc.core.dao.mysql.wrapper.QueryWrapper;
import com.leenmvc.core.security.User;
import com.leenmvc.core.security.entity.core.SysLogError;
import com.leenmvc.core.security.entity.export.LogErrorExcel;
import com.leenmvc.core.security.service.core.SysLogErrorService;
import com.leenmvc.core.utils.ConvertUtils;
import com.leenmvc.core.utils.ExcelUtils;
import com.leenmvc.core.base.result.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@RestController
@ApiDescription("异常日志控制器")
@RequestMapping("system/sysLogError")
public class SysLogErrorController extends BaseController<SysLogError> {

    @Autowired
    SysLogErrorService sysLogErrorService;


    @DeleteMapping("deleteAll")
    @RequiresPermissions("system:sysLogError:deleteAll")
    @Note("删除当前用户下的所有异常日志信息")
    public Result deleteAll(){
        delete("creator", User.getId());
        return new Result();
    }


    @GetMapping("export")
    @RequiresPermissions("system:sysLogError:export")
    @Note("导出异常信息excel文件")
    public void export(@RequestParam Map<String,Object> params, HttpServletResponse response) throws IOException {
        String fileName = String.valueOf(LocalDateTime.now());
        QueryWrapper wrapper = getQueryWrapper();
        wrapper.le("createDate",params.get("endTime"));
        wrapper.ge("createDate",params.get("startTime"));
        if(null != params.get("status") || !"".equals(params.get("status"))){
            wrapper.eq("status",params.get("status"));
        }
//        if(null != params.get("AL_creatorName") || !"".equals(params.get("AL_creatorName"))){
//            wrapper.eq("creatorName",params.get("AL_creatorName"));
//        }
        wrapper.eq("creator",User.getId());
        List<Object> logs = sysLogErrorService.query(wrapper);
        List<LogErrorExcel> logErrorExcels = ConvertUtils.sourceToTarget(logs, LogErrorExcel.class);
        ExcelUtils.exportExcel(response, fileName, logErrorExcels, LogErrorExcel.class);
    }

}
